package com.ly.hotel.spainterface.jyt.utils;

import android.os.Handler;
import android.os.Message;

import com.squareup.okhttp.Call;
import com.squareup.okhttp.Callback;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

/**
 * Created by ThinkPad on 2018/3/17.
 */

public class OkHttpUtils {

    private static OkHttpUtils okHttpUtils;
    private OkHttpClient mHttpClient;
    private Handler mHandler;

    private OkHttpUtils(){
         mHttpClient = new OkHttpClient();
         mHttpClient.setConnectTimeout(10, TimeUnit.SECONDS);
         mHttpClient.setReadTimeout(10,TimeUnit.SECONDS);
         mHttpClient.setWriteTimeout(10,TimeUnit.SECONDS);
         mHandler = new Handler();
    }

    public static OkHttpUtils getInstance(){
        if(okHttpUtils == null){
            okHttpUtils = new OkHttpUtils();
        }
        return okHttpUtils;
    }

    // 请求文本资源(get异步)
    public void getTextResource(String url,final Handler handler, final int judgeGoodsNum){
        Request request = new Request.Builder().url(url).build();
        Call call = mHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Request request, IOException e) {
                Message message = new Message();
                message.what = 2;
                message.arg2 = judgeGoodsNum;
                handler.sendMessage(message);
            }

            @Override
            public void onResponse(Response response) throws IOException {
                if(response.isSuccessful()){
                    String responseString = response.body().string();
                    Message message = new Message();
                    message.what = 1;
                    message.arg1 = judgeGoodsNum;
                    message.obj = responseString;
                    handler.sendMessage(message);
                }
            }
        });
    }

    /**
     * 异步请求数据
     */
    public static void getAsynData(String url,DataCallBack callBack,int requestCode){
        getInstance().inner_getAsynData(url,callBack,requestCode);
    }

    private void inner_getAsynData(String url, final DataCallBack callBack, final int requestCode){
        final Request request = new Request.Builder().url(url).build();
        mHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Request request, IOException e) {
                deliverDataFailtrue(request,e,callBack,requestCode);
            }

            @Override
            public void onResponse(Response response) throws IOException {
                String result = null;
                try {
                    result = response.body().string();
                } catch (IOException e) {
                    deliverDataFailtrue(request,e,callBack,requestCode);
                }
                deliverDataSuccess(result,callBack,requestCode);
            }
        });
    }

    /**
     * 分发失败的时候调用
     */
    private void deliverDataFailtrue(final Request request, final IOException e,
                                     final DataCallBack callBack, final int requestCode){
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                if(callBack != null){
                    callBack.requestFailure(request,e,requestCode);
                }
            }
        });
    }

    /**
     * 分发成功的时候调用
     */
    private void deliverDataSuccess(final String result, final DataCallBack callBack,
                                    final int requestCode){
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                if(callBack != null){
                    try {
                        callBack.requestSuccess(result,requestCode);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        });
    }

}
